package com.sinothk.gpserver.config.init;

import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sinothk.gpserver.modules.biz.entity.RoomEntity;
import com.sinothk.gpserver.modules.biz.mapper.RoomMapper;
import com.sinothk.gpserver.modules.biz.service.IRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import java.util.List;

/**
 * 编写 Liang Y.T.
 * 时间 2021/09/15 17:00
 */
@Component
public class RoomInit implements ServletContextAware {

    @Autowired
    private IRoomService roomService;

    /**
     * 在填充普通bean属性之后但在初始化之前调用
     * 类似于initializingbean的afterpropertiesset或自定义init方法的回调
     */
    @Override
    public void setServletContext(ServletContext servletContext) {
        LambdaQueryWrapper<RoomEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoomEntity::getDeleted, 0);
        List<RoomEntity> list = roomService.list(wrapper);
        list.forEach(room -> {
            roomService.initImRoom(room);
        });
        System.out.println("RoomIni_初始化房间: 将数据库房间加载到内存中");
    }
}